<?php
/**
 * Created by PhpStorm.
 * User: ovo
 * Date: 2018/3/7
 * Time: 下午6:08
 */
namespace app\home\controller;
use app\admin\model\UserModel;
use app\home\model\SmsModel;
use app\admin\model\AccountModel;
use app\admin\model\WithdrawalsModel;
use app\admin\model\User_pool;
use think\Db;
use think\Controller;
class User extends Controller
{
    function _initialize(){

        $this->user=model('admin/UserModel');
        $this->uid=session('uid');
    }
    #获取账户信息 提现
    public function index(){
        $type=input('param.type');
         if(empty($type)){
            return json(['status'=>302,'message'=>'參數錯誤']);
        }
        $user=$this->user->where(['id'=>$this->uid])->field('id,pool,activity_pool,phone,two_password,stack,truename,nickname')->find();
        #最小提现数
            $withdrawals_little_money=db('config')->where(['id'=>1])->value('withdrawals_little_money');
        if($type==1){
            
            $beginToday=date('Y-m-d')." 00:00:00";
            $endToday=date('Y-m-d H:i:s',strtotime($beginToday)+86399);
            $todayMoney = db('user_account')->where(['uid'=>$this->uid])->whereIn('type',[3,4])->whereBetween('create_time',[$beginToday,$endToday])->sum('money')?:'0.0000';
            return json(['status'=>'200','data'=>$user,'little_money'=>$withdrawals_little_money,'today_money'=>(string)$todayMoney,'message'=>'成功']);
        }elseif ($type==2) {
            $money=input('param.money/d');
            $wallet=input('param.wallet/s'); //钱包
            $two_password=input('param.two_password');
            $code=input('param.code');
            if($wallet !=$user['stack']){
                return json(['status'=>302,'message'=>'請輸入自己的錢包地址']);
            }
            if($money<$withdrawals_little_money){
                return json(['status'=>302,'message'=>'提現小于最小提現金額'.$withdrawals_little_money]);
            }
            #手续费
            $procedures=db('config')->where(['id'=>1])->value('procedures');
            if(!in_array($user['phone'], config('phones'))){
                $codes=SmsModel::where(['type'=>3,'phone'=>$user['phone']])->order('id desc')->find();
                if($code !=$codes['code']){
                    return json(['status'=>302,'message'=>'驗證碼錯誤']);
                }else{
                    SmsModel::where(['code'=>$code,'type'=>3])->update(['status'=>1]);
                }
            }
            if(md5($two_password)!= $user['two_password']){
                return json(['status'=>302,'message'=>'支付密碼錯誤']);
            }
            if($money >$user['activity_pool']){
                return json(['status'=>302,'message'=>'賬戶余額不足']);
            }
            #转出记录
            $z_activity=[];
            $z_activity['uid']=$this->uid;
            $z_activity['source_id']=$this->uid;
            $z_activity['money']=$money;
            $z_activity['balance']=$user['activity_pool'] -$money;
            $z_activity['message']='活動賬戶提現';
            $z_activity['type']=1;
            $z_activity['status']=2;
            $z_activity['class']=2;
            $z_activity['create_time']=date('Y-m-d H:i:s',time());
            AccountModel::insert($z_activity);
            $this->user->where(['id'=>$this->uid])->setDec('activity_pool',$money);
            $data=[];
            $data['uid']=$this->uid;
            if(!$user['truename']){
                $data['name']=$user['truename'];
            }
            $data['account']=$wallet; //钱包地址
            $data['status']=1;
            $data['reality_money']= $money * (1-$procedures) * 100;  //实际到账金额
            $data['money']=$money;
            $data['total_account']=$user['activity_pool'] -$money;  // 账户余额  
            $data['discount']= $procedures;        //手续费
            $data['pay_num']='Y'.time().'z'.time();
            $data['create_time']=date('Y-m-d H:i:s');
            $res=WithdrawalsModel::insert($data);
            return json(['status'=>200,'message'=>'成功']);
        }
    }

    #平台内互转 
    public function  accounts(){
        #对方账户
        $data=[];
        $pphone=input('param.pphone');
        $stack=input('param.stack'); //钱包地址
        #最小转账
        $account_little_money=db('config')->where(['id'=>1])->value('account_little_money');
        $user=$this->user->where(['id'=>$this->uid])->field('id,pool,pid,activity_pool,phone,two_password,truename')->find();
        // $code=input('param.code');
        $two_password=input('param.two_password');
        $code=input('param.code');
        if(!in_array($user['phone'], config('phones'))){
            $codes=SmsModel::where(['type'=>7,'phone'=>$user['phone']])->order('id desc')->find();
       
            if($code != $codes['code']){
                return json(['status'=>302,'message'=>'驗證碼錯誤']);
            }else{
                SmsModel::where(['code'=>$code,'type'=>7])->update(['status'=>1]);
            }
        }
        // $puser=$this->user->where(['phone'=>$pphone])->field('id,pool,pid,activity_pool,phone,two_password,truename')->find();
        $puser=$this->user->where(['stack'=>$stack])->field('id,pool,pid,activity_pool,phone,two_password,truename')->find();
        if(empty($puser)){
            return json(['status'=>302,'message'=>'賬戶不存在，核實後在嘗試']);
        }
        $pool=0;
        if(!empty(input('param.pool'))){ //矿池
            $pool=input('param.pool/d');
            if($account_little_money > $pool){
                return json(['status'=>302,'message'=>'轉賬小于最小提現金額'.$account_little_money]);
            }
            if($user['pool'] < $pool){
                return json(['status'=>302,'message'=>'礦池金幣不足']);
            }   
        }
        $activity_pool=0;
        if(!empty(input('param.activity_pool'))){//转账 活动
            $activity_pool=input('param.activity_pool/d');
            if($account_little_money > $activity_pool){
                return json(['status'=>302,'message'=>'轉賬小于最小提現金額'.$account_little_money]);
            }
            return json(['status'=>302,'message'=>'活動賬戶轉出功能暫時關閉']);
            
            if($user['activity_pool'] < $activity_pool){
                return json(['status'=>302,'message'=>'活動金幣不足']);
            } 
        }
        // #判断是否直属关系
        // if($user['id']!=$puser['pid']  &&  $user['pid'] != $puser['id']){
        //     return json(['status'=>302,'message'=>'無直屬關系']);
        // }

        #判断是否直属关系
        if($user['id']  == $puser['id']){
            return json(['status'=>302,'message'=>'不能轉自身']);
        }
        if($pool==0 && $activity_pool==0 ){
            return json(['status'=>302,'message'=>'請輸入金額']);
        }
        if(md5($two_password) != $user['two_password'] || $two_password==''){
            return json(['status'=>302,'message'=>'交易密碼錯誤']);
        }

        
        #
        if(!empty($pool) && !in_array($user['phone'], config('phones'))){
            $sumpool = User_pool::where(['uid'=>$user['id'],'one_balance'=>['>',0]])->sum('one_balance');
            if($sumpool >$pool ){
                $res=$this->to_pool($user['id'],$puser['id'],$pool);
                if($res){
                    $ress=$this->activityhk($user['id'],$puser['id'],$pool,2);//添加账户记录
                }
            }else{
                return json(['status'=>302,'message'=>'礦池金幣不足']);
            }
            
        }

        if(in_array($user['phone'], config('phones'))){
           
            $res=$this->to_pool($user['id'],$puser['id'],$pool);
            if($res){

                $ress=$this->activityhk($user['id'],$puser['id'],$pool,2);//添加账户记录

            }
        }
        // if(!empty($activity_pool)){
        //     $ress=$this->activity($user['id'],$puser['id'],$activity_pool,1);
        // }
        //if($ress){
            return json(['status'=>200,'message'=>'幣已經成功轉入對方賬戶']);
        // }else{
        //     return json(['status'=>302,'message'=>'幣已經成功']);
        // }
    }

    #活动账户添加记录
    public function activity($uid,$puid,$money,$type){

            $activity_pool=$this->user->where(['id'=>$uid])->value('activity_pool');
            $pactivity_pool=$this->user->where(['id'=>$puid])->value('activity_pool');
            #转入记录
            $activity=[];
            $activity['uid']=$puid;
            $activity['source_id']=$uid;
            $activity['money']=$money ;
            $activity['balance']=$pactivity_pool + $money ;
            $activity['message']='活動賬戶轉入';
            $activity['type']=$type;
            $activity['status']=2;
            $activity['class']=1;
            $activity['create_time']=date('Y-m-d H:i:s',time());

            AccountModel::insert($activity);
            #转出记录
            $z_activity=[];
            $z_activity['uid']=$uid;
            $z_activity['source_id']=$puid;
            $z_activity['money']=$money;
            $z_activity['balance']=$activity_pool -$money;
            $z_activity['message']='活動賬戶轉出';
            $z_activity['type']=$type;
            $z_activity['status']=2;
            $z_activity['class']=2;
            $z_activity['create_time']=date('Y-m-d H:i:s',time());
            AccountModel::insert($z_activity);
            $this->user->where(['id'=>$puid])->setInc('activity_pool',$money);
            $this->user->where(['id'=>$uid])->setDec('activity_pool',$money);
            return 111;
    }



    #矿池转入转出添加记录
    public function activityhk($uid,$puid,$money,$type){

            $pool=$this->user->where(['id'=>$uid])->value('pool');
            $p_pool=$this->user->where(['id'=>$puid])->value('pool');
        // Db::startTrans();
        // try {
            #转入记录
            $activity=[];
            $activity['uid']=$puid;
            $activity['source_id']=$uid;
            $activity['money']=$money ;
            $activity['balance']=$p_pool + $money ;
            $activity['message']='礦池賬戶轉入';
            $activity['type']=2;
            $activity['status']=2;
            $activity['class']=1;
            $datas['unlock_date']=date('Y-m-d H:i:s', strtotime("+90 day"));
            $activity['create_time']=date('Y-m-d H:i:s',time());

            AccountModel::insert($activity);
            #转出记录
            $z_activity=[];
            $z_activity['uid']=$uid;
            $z_activity['source_id']=$puid;
            $z_activity['money']=$money;
            $z_activity['balance']=$pool -$money;
            $z_activity['message']='礦池賬戶轉出';
            $z_activity['type']=2;
            $z_activity['status']=2;
            $z_activity['class']=2;
            $z_activity['create_time']=date('Y-m-d H:i:s',time());
            AccountModel::insert($z_activity);
            $this->user->where(['id'=>$puid])->setInc('pool',$money);
            $this->user->where(['id'=>$uid])->setDec('pool',$money);

              #添加到矿池
            $datas=[];
            $datas['uid']=$puid;
            $datas['order_sn']='Y'.time();
            $datas['source_id']=$uid;
            $datas['source_msg']='礦池賬戶轉入';
            $datas['use_money']=0;
            $datas['money']=$money;
            $datas['balance']=$p_pool + $money;
            $datas['one_balance']=$money;
            $datas['unlock_money']=$money;
            $datas['unlock_date']=date('Y-m-d H:i:s', strtotime("+90 day"));
            $datas['create_time']=date('Y-m-d H:i:s', time());
            $datas['update_time']=date('Y-m-d H:i:s', time());
            User_pool::insert($datas);
             #自身
            $valid_value=db('config')->where(['id'=>1])->value('valid_value'); //最小数值
                    $userpoll=$this->user->where(['id'=>$uid])->field('type,pool')->find();
                    if($userpoll['pool'] < $valid_value && $userpoll['type']!=1){//如果少于$valid_value成为无效用户
                        $this->user->where(['id'=>$uid])->update(['type'=>1,'update_time'=>date('Y-m-d H:i:s', time())]);
                        db('user_position')->where(['uid'=>$uid])->update(['is_effective'=>1,'update_time'=>date('Y-m-d H:i:s', time())]);
                    }
                    #她人
                    $puserpoll=$this->user->where(['id'=>$puid])->field('type,pool')->find();
                    if($puserpoll['pool'] >= $valid_value && $puserpoll['type']!=2){//如果多于$valid_value成为有效用户
                        $this->user->where(['id'=>$puid])->update(['type'=>2,'update_time'=>date('Y-m-d H:i:s', time())]);
                        db('user_position')->where(['uid'=>$puid])->update(['is_effective'=>2,'update_time'=>date('Y-m-d H:i:s', time())]);
                    }
        //  Db::commit();
        // } catch (Exception $e) {
        //     Db::rollback();
        //     return 0;
        // }
            return 111;
    }

    #递归扣除奖池金币
    public  function to_pool($uid,$puid,$money){  //uid的金币转入puid账户中

        $arr = User_pool::where(['uid'=>$uid,'one_balance'=>['>',0]])->order('id','asc')->find();

        if($arr['one_balance'] >=  $money){
                $pool=$this->user->where(['id'=>$uid])->value('pool'); //用户总额
                $surplus_monry=$money-$arr['one_balance'];
                if($arr['one_balance']==$money){
                    #更新数据
                    $dada=[];
                    $dada['one_balance']=$surplus_monry; //此条记录
                    $dada['balance']=$pool-$money;
                    $dada['use_money']=$money + $arr['use_money'];
                    User_pool::where(['id'=>$arr['id']])->update($dada);
                }elseif($arr['one_balance']>$money){
                    #更新数据
                    $dada=[];
                    $dada['one_balance']=$arr['one_balance']-$money;
                    $dada['balance']=$pool-$money;
                    $dada['use_money']=$money + $arr['use_money'];
                    User_pool::where(['id'=>$arr['id']])->update($dada);
                }
                return 111;  
            }elseif($arr['one_balance'] < $money){
                #更新数据
                $surplus_monry= $money-$arr['one_balance'];
                $dada=[];
                $dada['one_balance']=0;
                $dada['use_money']=$arr['money'];
                $res=User_pool::where(['id'=>$arr['id']])->update($dada);
                #进行下一轮
                $nextmoney=$money - $arr['one_balance'];
                if($res){
                    return  $this->to_pool($uid,$puid,$nextmoney);
                }
            }  
    }
    /***********************************************************8*/
    #矿池转入记录
    public function pool_to_record(){
        #分页
        $p=input('param.p/d');
        $pagecount=10;
        $start = $p>0 ? ($p - 1) * $pagecount : 0;
        $record=User_pool::where(['uid'=>$this->uid])->field('id,create_time,unlock_date,money,balance')->limit($start,$pagecount)->order('id desc')->select();
        return json(['start'=>200,'data'=>$record,'message'=>'獲取數據成功']);
    }
    #提现记录
    public function present_record(){
        #分页
        $p=input('param.p/d');
        $type=input('param.type');
         if(empty($type)){
            return json(['status'=>302,'message'=>'參數錯誤']);
        }
        $start_time=input('param.start_time');
        $end_time=input('param.end_time');
        $wheres=[];
        if($start_time && $end_time){
            $wheres['create_time']=['between',[$start_time,$end_time]];
        }
        $pagecount=10;
        $start = $p>0 ? ($p - 1) * $pagecount : 0;
        if($type==1){ # 状态1提交,2通过,3驳回 4全部
            $record=WithdrawalsModel::where(['uid'=>$this->uid,'status'=>1])->where($wheres)->field('id,create_time,reality_money,money,total_account,status')->limit($start,$pagecount)->order('id desc')->select();
        }elseif ($type==2) {
            $record=WithdrawalsModel::where(['uid'=>$this->uid,'status'=>2])->where($wheres)->field('id,create_time,reality_money,money,total_account,status')->limit($start,$pagecount)->order('id desc')->select();
        }elseif ($type==3) {
           $record=WithdrawalsModel::where(['uid'=>$this->uid,'status'=>3])->where($wheres)->field('id,create_time,reality_money,money,total_account,status')->limit($start,$pagecount)->order('id desc')->select(); 
        }elseif ($type==4) {
            $record=WithdrawalsModel::where(['uid'=>$this->uid])->where($wheres)->field('id,create_time,money,reality_money,total_account,status')->limit($start,$pagecount)->order('id desc')->select();
        }
        return json(['start'=>200,'data'=>$record,'message'=>'獲取數據成功']);
    }
    #收益记录
    public function revenue_record(){
        #分页
        #  1  3:静态分红  2  4:代数静态分红百分比  3:礦池每日收益  4:節點分紅
        $p=input('param.p/d');
        $type=input('param.type');
        if(empty($type)){
            return json(['status'=>302,'message'=>'參數錯誤']);
        }
        $start_time=input('param.start_time');
        $end_time=input('param.end_time');
        $wheres=[];
        if($start_time && $end_time){
            $wheres['create_time']=['between',[$start_time,$end_time]];
        }
        $pagecount=10;
        $start = $p>0 ? ($p - 1) * $pagecount : 0;
        if($type==1){
            $record=AccountModel::where(['uid'=>$this->uid,'type'=>3])->where($wheres)->field('id,create_time,money,balance,message,class,type')->limit($start,$pagecount)->order('id desc')->select();
        }elseif($type==2){
            $record=AccountModel::where(['uid'=>$this->uid,'type'=>4])->where($wheres)->field('id,create_time,money,balance,message,class,type')->limit($start,$pagecount)->order('id desc')->select();
        }elseif($type==3){
            $record=AccountModel::where(['uid'=>$this->uid,'type'=>['in',[3,4]]])->where($wheres)->field('id,create_time,money,balance,message,class,type')->limit($start,$pagecount)->order('id desc')->select();
        }
        return json(['start'=>200,'data'=>$record,'message'=>'獲取數據成功']);
    }


    /****************************************************************************/
    #转入转出  内转记录
    public function in_turn_record(){
        
        $p=input('param.p/d');
        $type=input('param.type');
         if(empty($type)){
            return json(['status'=>302,'message'=>'參數錯誤']);
        }
        $start_time=input('param.start_time');
        $end_time=input('param.end_time');
        $wheres=[];
        if($start_time && $end_time){
            $wheres['create_time']=['between',[$start_time,$end_time]];
        }
        $pagecount=10;
        $start = $p>0 ? ($p - 1) * $pagecount : 0;
        $where=[1,2];
        if($type==1){  //转入
            $record=AccountModel::where(['uid'=>$this->uid,'type'=>['in',$where],'class'=>1,'source_id'=>['<>',$this->uid]])->where(['source_id'=>['<>',0]])->where($wheres)->field('source_id,money,balance,message,create_time,uid,class')->limit($start,$pagecount)->order('id desc')->select();
            foreach ($record as $key => $value) {
                // $record[$key]['phone']=UserModel::where(['id'=>$value['source_id']])->value('phone');
                $record[$key]['phone']=UserModel::where(['id'=>$value['source_id']])->value('stack');
            }
        }elseif ($type==2) {//转出
            $record=AccountModel::where(['uid'=>$this->uid,'type'=>['in',$where],'class'=>2,'source_id'=>['<>',$this->uid]])->where(['source_id'=>['<>',0]])->where($wheres)->field('source_id,money,balance,message,create_time,uid,class')->limit($start,$pagecount)->order('id desc')->select();
             foreach ($record as $key => $value) {
                // $record[$key]['phone']=UserModel::where(['id'=>$value['source_id']])->value('phone');
                $record[$key]['phone']=UserModel::where(['id'=>$value['source_id']])->value('stack');
            }
        }elseif($type==3){
            $record=AccountModel::where(['uid'=>$this->uid,'type'=>['in',$where],'source_id'=>['<>',$this->uid]])->where(['source_id'=>['<>',0]])->where($wheres)->field('source_id,money,balance,message,create_time,uid,class')->limit($start,$pagecount)->order('id desc')->select();
             foreach ($record as $key => $value) {
                $record[$key]['phone']=UserModel::where(['id'=>$value['source_id']])->value('phone');
            }
        }
        return json(['status'=>200,'data'=>$record,'message'=>'內轉記錄']);
    }

    /*****************************************************************************************************/
    #矿池转出
    public function pool_roll_out(){
        $type=input('param.type');
         if(empty($type)){
            return json(['status'=>302,'message'=>'參數錯誤']);
        }
        #矿池转出比例
        $pool_to_momey=db('config')->where(['id'=>1])->value('pool_to_momey');
        #查询用户可转出的金额
        #（剩余金额）每30天可转一次的，
        $time=date('Y-m-d H:i:s',time());//当前时间
        $arr = User_pool::where(['uid'=>$this->uid,'one_balance'=>['>',0],'unlock_date'=>['<=',$time]])->order('id','asc')->select();
        #定义金额
        $money=0;
        $num=0;
        //#存放所有可转金额的数据id
        $arrayid=[];
       
        foreach ($arr as $key => $value) {
                $arrayid[$key]=$value['id'];
                $money+=$value['one_balance']-$value['unlock_money'];
        }
        #此次可转出数量    
        $money=floor($money); //向下取整
        if($money<0){
            $money=0;
        }

        if($type==1){ #返回可提现金额
            return json(['status'=>200,'money'=>$money,'message'=>'可轉金額']);
        }elseif($type==2){#执行转出操作

            if(empty($arrayid)){
                return json(['status'=>302,'message'=>'礦池金額無解鎖記錄']);
            }
            $moneys=input('param.money');
            $two_password=input('param.two_password');
            $code=input('param.code');
            $account_little_money=db('config')->where(['id'=>1])->value('account_little_money');
            if($account_little_money > $moneys){
                return json(['status'=>302,'message'=>'轉賬小于最小提現金額'.$account_little_money]);
            }
            $user=$this->user->where(['id'=>$this->uid])->field('id,pool,pid,activity_pool,phone,two_password,truename')->find();
             if(md5($two_password) != $user['two_password'] || $two_password==''){
                    return json(['status'=>302,'message'=>'交易密碼錯誤']);
                }

                if($money < $moneys || $moneys==0){
                        return json(['status'=>302,'message'=>'金額不足']);
                }
                // $codes=SmsModel::where(['type'=>7,'phone'=>$user['phone']])->order('id desc')->find();
                // if($code !=$codes['code']){
                //     return json(['status'=>302,'message'=>'驗證碼錯誤']);
                // }else{
                //     SmsModel::where(['code'=>$code,'type'=>7])->update(['status'=>1]);
                // }
                // foreach ($arr as $k => $v) {
                //     $start_time = $v['unlock_date'];
                //     #当前时间减去解锁时间
                //     $day=time()-strtotime($start_time);
                //     $day=floor($day / 86400 /30)+1;
                //     if($day > $v['pool_roll_num']){
                //         #计算金额
                //         $money+=$v['one_balance'] * $pool_to_momey / 100;
                //         $arrayid[$k]=$v['id'];
                //         //$num=$day-$v['pool_roll_num'];
                //     }
                // }
             #数组id,金额，比例                       
            $res=$this->do_pool_roll_out($this->uid,$arrayid,$moneys,$pool_to_momey);
            return json(['status'=>200,'message'=>'轉出成功']);
        }
    }

    #执行扣除矿池币，添加活动币操作
    public  function do_pool_roll_out($uid,$arrayid,$money,$pool_to_momey){
        #定义新金额 矿池记录转出时 要转总量的剩余量
        $arr = User_pool::where(['uid'=>$uid,'one_balance'=>['>',0],'id'=>['in',$arrayid]])->order('id','asc')->find();

        $max=$arr['one_balance']-$arr['unlock_money'];
        if($max >=$money){
            $arr = User_pool::where(['uid'=>$uid,'one_balance'=>['>',0]])->order('id','asc')->find();
              #如果这个记录 的最大转出量多于 要转的金币量 直接返回,并执行添加记录
                $surplus_monry=$arr['one_balance']-$money;
                    #执行
                    if($arr['one_balance']==$money){
                            #更新数据
                            $dada=[];
                            $dada['pool_roll_num']=$arr['pool_roll_num'] +1;
                            $dada['one_balance']=$surplus_monry; //此条记录
                            $dada['balance']=$arr-$money;
                            $dada['use_money']=$money + $arr['use_money'];
                            User_pool::where(['id'=>$arr['id']])->update($dada);
                        }elseif($arr['one_balance']>$money){
                            #更新数据
                            $dada=[];
                            //$dada['balance']= $arr['balance'] -$money;
                            $dada['pool_roll_num']=$arr['pool_roll_num'] +1;
                            $dada['one_balance']=$surplus_monry;
                            $dada['use_money']=$money + $arr['use_money'];
                            User_pool::where(['id'=>$arr['id']])->update($dada);
                        }
                    #TODO //添加活动账户记录
                    $this->activitys($arr['uid'],$money);
                    #自减用户矿池币
                    $userpoll=$this->user->where(['id'=>$arr['uid']])->value('pool');
                    $valid_value=db('config')->where(['id'=>1])->value('valid_value'); //最小数值
                    if($userpoll < $valid_value){//如果少于$valid_value成为无效用户
                        $this->user->where(['id'=>$arr['uid']])->update(['type'=>1]);
                        db('user_position')->where(['uid'=>$arr['uid']])->update(['is_effective'=>1]);
                    }
                return 111;
            }elseif($max < $money){
                 #下一条要转的金额
                    #更新数据
                    $surplus_monry= $money-$arr['one_balance'];
                    $dada=[];
                    $dada['one_balance']=0;
                    $dada['use_money']=$arr['money'];
                    User_pool::where(['id'=>$arr['id']])->update($dada);
                     foreach($arrayid as $k=>$v){  
                        if($v == $arr['id']){  
                            unset($arrayid[$k]);  
                        }  
                    } 
                    return $this->do_pool_roll_out($uid,$arrayid,$surplus_monry,$pool_to_momey);
            }
    }

    #矿池转出 添加活动账户转入记录执行
    public  function activitys($uid,$money){
        $activity_pool=$this->user->where(['id'=>$uid])->find();
        #转入记录
        $s=[];
        $activitys['uid']=$uid;
        $activitys['source_id']=$uid;
        $activitys['money']=$money ;
        $activitys['balance']=$activity_pool['activity_pool'] + $money ;
        $activitys['message']='活動賬戶轉入';
        $activitys['type']=1;
        $activitys['status']=2;
        $activitys['class']=1;
        $activitys['create_time']=date('Y-m-d H:i:s',time());
        AccountModel::insert($activitys);
        #转出记录
        $activity=[];
        $activity['uid']=$uid;
        $activity['source_id']=$uid;
        $activity['money']=$money ;
        $activity['balance']=$activity_pool['pool'] - $money ;
        $activity['message']='礦池賬戶轉出';
        $activity['type']=2;
        $activity['status']=2;
        $activity['class']=2;
        $activity['create_time']=date('Y-m-d H:i:s',time());
        AccountModel::insert($activity);
        $this->user->where(['id'=>$uid])->setDec('pool',$money);
        $this->user->where(['id'=>$uid])->setInc('activity_pool',$money);
    }
    
    
    
}